//
//  UIView+LineView.m
//  UIView添加下划线
//
//  Created by Haitang on 16/7/13.
//  Copyright © 2016年 Haitang. All rights reserved.
//

#import "UIView+LineView.h"

@implementation UIView (LineView)


- (void)setLineWithDirection:(TGLINEDIRECTION) direction  Color:(UIColor *)lineColor Height:(CGFloat )height Width:(CGFloat) width{
    CGSize size = self.bounds.size;
    CALayer *lineLayer = [CALayer layer];
    [lineLayer setBackgroundColor:lineColor.CGColor];
    switch (direction) {
        case TGLINETOP:{
            [lineLayer setFrame:CGRectMake(0, 0, size.width, height)];
        }
            break;
        case TGLINELEFT:{
            [lineLayer setFrame:CGRectMake(0, 0, width, size.height)];
            
        }
            break;
        case TGLINERIGHT:
        {
            [lineLayer setFrame:CGRectMake(size.width-width, 0, width, size.height)];
        }
            break;
        case TGLINEBOTTOM:
        {
            [lineLayer setFrame:CGRectMake(0, size.height - height, size.width, height)];
        }
            break;
        default:
            break;
    }
    [self.layer addSublayer:lineLayer];
}

/**
 ** lineView:       需要绘制成虚线的view
 ** lineLength:     虚线的宽度
 ** lineSpacing:    虚线的间距
 ** lineColor:      虚线的颜色
 **/
- (void)drawDashLineWithlineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor
{
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    [shapeLayer setBounds:self.bounds];
    [shapeLayer setPosition:CGPointMake(CGRectGetWidth(self.frame) / 2, CGRectGetHeight(self.frame))];
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    [shapeLayer setLineWidth:CGRectGetHeight(self.frame)];
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    CGPathAddLineToPoint(path, NULL,CGRectGetWidth(self.frame), 0);
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [self.layer addSublayer:shapeLayer];
}
@end
